﻿using Emoney.UserTraining.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Linq;

namespace Emoney.UserTraining.DAL
{
    public class Training_MeetingUserDAL
    {
        /// <summary>
        /// 预约网络会议(返回成功后主键值)
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public int AddUser(Training_MeetingUser c)
        { 
            using(TemporaryContext context=new TemporaryContext())
            {
                var query = from a in context.Training_MeetingUser
                            where a.username == c.username && a.netid == c.netid && a.isOrder == "0"
                            select a;
                List<Training_MeetingUser> lst = query.ToList();
                if (lst.Count > 0)
                {
                    //取消预约isorder为0，再次预约时操作
                    var tb = context.Training_MeetingUser;
                    int id = lst[0].id;
                    var f = tb.FirstOrDefault(x => x.id == id);
                    f.uid = c.uid;
                    f.isOrder = "1";
                    f.Ordertime = DateTime.Now;
                    context.Session.SaveOrUpdate(f);
                    context.Session.Flush();
                    return id;
                }
                else
                {
                    var query1 = from a in context.Training_MeetingUser
                                 where a.username == c.username && a.netid == c.netid && a.isOrder == "1"
                                 select a;
                    List<Training_MeetingUser> lst1 = query1.ToList();
                    if (lst1.Count > 0)//已预约过不能重复预约
                    {
                        return -1;
                    }
                    else
                    {
                        object o = context.Session.Save(c);
                        context.Session.Flush();
                        return int.Parse(o.ToString());
                    }
                }
            }
        }
        //查询网络会议预约记录
        public List<Training_MeetingUser> GetLstOrderUser(Training_MeetingUser c)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                var query = from a in context.Training_MeetingUser
                            where a.username == c.username && a.netid == c.netid && a.isOrder == "1"
                            select a;
                List<Training_MeetingUser> lst = query.ToList();
                return lst;
            }
        }
        /// <summary>
        /// 修改预约信息(30分钟提醒）
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public int ModifyUser(int id,string is30tip,DateTime time)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                var tb = context.Training_MeetingUser;
                var c = tb.FirstOrDefault(x => x.id == id);
                c.is30tip = is30tip;
                c.is30tiptime = time;
                context.Session.Update(c);
                context.Session.Flush();
                return 1;
            }
        }
        /// <summary>
        /// 取消网络会议预约
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isOrder"></param>
        /// <returns></returns>
        public int ModifyUserById(int id, string isOrder)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                try
                {
                    var tb = context.Training_MeetingUser;
                    var c = tb.FirstOrDefault(x => x.id == id);
                    c.isOrder = isOrder;
                    c.cancelOrdertime = DateTime.Now;
                    c.is30tip = "0";
                    context.Session.SaveOrUpdate(c);
                    context.Session.Flush();
                    return 1;
                }
                catch
                {
                    return 0;
                }
            }
        }
       

        public List<Training_MeetingUser> GetLstByUserName(string userName)
        {
          
            using (TemporaryContext context = new TemporaryContext())
            {
                var query = from a in context.Training_MeetingUser
                            from b in context.Traning_NetworkMeeting
                            where a.netid == b.Id.ToString() && a.username == userName
                            select a;
                return query.Cacheable().ToList();
            }
        }
        public List<TrainNetwoekAndUser> GetLstByUser(string userName)
        {

            using (TemporaryContext context = new TemporaryContext())
            {
                var query = from a in context.Training_MeetingUser
                            from b in context.Traning_NetworkMeeting
                            where a.netid == b.Id.ToString() && a.username == userName && a.isOrder=="1"
                            select new TrainNetwoekAndUser { 
                                Id = b.Id,
                                orderid=a.id,
                                mtg_id=b.mtg_id,
                                netid = a.netid,
                                before_time = b.before_time,
                                class_date = b.class_date, 
                                endDate = b.endDate,
                                Duration_date = b.Duration_date,
                                mtg_name = b.mtg_name,
                                meeting_type = b.meeting_type,
                                txtteachar = b.txtteachar,
                                video_url = b.video_url
                            };
                return query.Cacheable().ToList();
            }
        }

        public List<TrainNetwoekAndUser> GetLstByUid(string uid)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                var query = from a in context.Training_MeetingUser
                            from b in context.Traning_NetworkMeeting
                            where a.netid == b.Id.ToString() && a.uid == uid && a.isOrder == "1" && a.is30tip == "1"
                            select new TrainNetwoekAndUser
                            {
                                Id = b.Id,
                                orderid = a.id,
                                mtg_id = b.mtg_id,
                                netid = a.netid,
                                before_time = b.before_time,
                                class_date = b.class_date,
                                endDate = b.endDate,
                                Duration_date = b.Duration_date,
                                mtg_name = b.mtg_name,
                                meeting_type = b.meeting_type,
                                txtteachar = b.txtteachar,
                                uid = a.uid
                            };
                return query.ToList();
            }
        }
        
    }
}
